﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using YXShop.IDAL.SystemInfo;

namespace YXShop.SQLServerDAL.SystemInfo
{
    /// <summary>
    /// 数据访问类ShopSetting。
    /// </summary>
    public class ShopSetting : IShopSetting
    {
        public ShopSetting()
        { }
        #region  成员方法

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(YXShop.Model.SystemInfo.ShopSetting model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into yxs_ShopSetting(");
            strSql.Append("AllowVisitorBuy,Coupon,Thumbnails,WaterMark,NumberLimit,OrdersReceive,OrdersText,Tel,Adress,Zip)");
            strSql.Append(" values (");
            strSql.Append("@AllowVisitorBuy,@Coupon,@Thumbnails,@WaterMark,@NumberLimit,@OrdersReceive,@OrdersText,@Tel,@Adress,@Zip)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@AllowVisitorBuy", SqlDbType.TinyInt,1),
					new SqlParameter("@Coupon", SqlDbType.TinyInt,1),
					new SqlParameter("@Thumbnails", SqlDbType.TinyInt,1),
					new SqlParameter("@WaterMark", SqlDbType.TinyInt,1),
					new SqlParameter("@NumberLimit", SqlDbType.Int,4),
					new SqlParameter("@OrdersReceive", SqlDbType.Int,4),
					new SqlParameter("@OrdersText", SqlDbType.VarChar,50),
					new SqlParameter("@Tel", SqlDbType.VarChar,50),
					new SqlParameter("@Adress", SqlDbType.VarChar,500),
					new SqlParameter("@Zip", SqlDbType.VarChar,50)};
            parameters[0].Value = model.AllowVisitorBuy;
            parameters[1].Value = model.Coupon;
            parameters[2].Value = model.Thumbnails;
            parameters[3].Value = model.WaterMark;
            parameters[4].Value = model.NumberLimit;
            parameters[5].Value = model.OrdersReceive;
            parameters[6].Value = model.OrdersText;
            parameters[7].Value = model.Tel;
            parameters[8].Value = model.Adress;
            parameters[9].Value = model.Zip;

            object obj = ChangeHope.DataBase.SQLServerHelper.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(YXShop.Model.SystemInfo.ShopSetting model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update yxs_ShopSetting set ");
            strSql.Append("AllowVisitorBuy=@AllowVisitorBuy,");
            strSql.Append("Coupon=@Coupon,");
            strSql.Append("Thumbnails=@Thumbnails,");
            strSql.Append("WaterMark=@WaterMark,");
            strSql.Append("NumberLimit=@NumberLimit,");
            strSql.Append("OrdersReceive=@OrdersReceive,");
            strSql.Append("OrdersText=@OrdersText,");
            strSql.Append("Tel=@Tel,");
            strSql.Append("Adress=@Adress,");
            strSql.Append("Zip=@Zip,");
            strSql.Append("allowgroupbuydeposit=@allowgroupbuydeposit,");
            strSql.Append("allowauctiondeposit=@allowauctiondeposit");
            strSql.Append(" where ShopId=@ShopId ");
            SqlParameter[] parameters = {
					new SqlParameter("@ShopId", SqlDbType.Int,4),
					new SqlParameter("@AllowVisitorBuy", SqlDbType.TinyInt,1),
					new SqlParameter("@Coupon", SqlDbType.TinyInt,1),
					new SqlParameter("@Thumbnails", SqlDbType.TinyInt,1),
					new SqlParameter("@WaterMark", SqlDbType.TinyInt,1),
					new SqlParameter("@NumberLimit", SqlDbType.Int,4),
					new SqlParameter("@OrdersReceive", SqlDbType.Int,4),
					new SqlParameter("@OrdersText", SqlDbType.VarChar,50),
					new SqlParameter("@Tel", SqlDbType.VarChar,50),
					new SqlParameter("@Adress", SqlDbType.VarChar,500),
					new SqlParameter("@Zip", SqlDbType.VarChar,50),
                    new SqlParameter("@allowgroupbuydeposit", SqlDbType.TinyInt,1),
                    new SqlParameter("@allowauctiondeposit", SqlDbType.TinyInt,1)};
            parameters[0].Value = model.ShopId;
            parameters[1].Value = model.AllowVisitorBuy;
            parameters[2].Value = model.Coupon;
            parameters[3].Value = model.Thumbnails;
            parameters[4].Value = model.WaterMark;
            parameters[5].Value = model.NumberLimit;
            parameters[6].Value = model.OrdersReceive;
            parameters[7].Value = model.OrdersText;
            parameters[8].Value = model.Tel;
            parameters[9].Value = model.Adress;
            parameters[10].Value = model.Zip;
            parameters[11].Value = model.AllowGroupbuyDeposit;
            parameters[12].Value = model.AllowAuctionDeposit;
            ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public YXShop.Model.SystemInfo.ShopSetting GetModel()
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ShopId,AllowVisitorBuy,Coupon,Thumbnails,WaterMark,NumberLimit,OrdersReceive,OrdersText,Tel,Adress,Zip,allowgroupbuydeposit,allowauctiondeposit from yxs_ShopSetting ");
            

            YXShop.Model.SystemInfo.ShopSetting model = new YXShop.Model.SystemInfo.ShopSetting();
            DataSet ds = ChangeHope.DataBase.SQLServerHelper.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ShopId"].ToString() != "")
                {
                    model.ShopId = int.Parse(ds.Tables[0].Rows[0]["ShopId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AllowVisitorBuy"].ToString() != "")
                {
                    model.AllowVisitorBuy = int.Parse(ds.Tables[0].Rows[0]["AllowVisitorBuy"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Coupon"].ToString() != "")
                {
                    model.Coupon = int.Parse(ds.Tables[0].Rows[0]["Coupon"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Thumbnails"].ToString() != "")
                {
                    model.Thumbnails = int.Parse(ds.Tables[0].Rows[0]["Thumbnails"].ToString());
                }
                if (ds.Tables[0].Rows[0]["WaterMark"].ToString() != "")
                {
                    model.WaterMark = int.Parse(ds.Tables[0].Rows[0]["WaterMark"].ToString());
                }
                if (ds.Tables[0].Rows[0]["NumberLimit"].ToString() != "")
                {
                    model.NumberLimit = int.Parse(ds.Tables[0].Rows[0]["NumberLimit"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OrdersReceive"].ToString() != "")
                {
                    model.OrdersReceive = int.Parse(ds.Tables[0].Rows[0]["OrdersReceive"].ToString());
                }
                model.OrdersText = ds.Tables[0].Rows[0]["OrdersText"].ToString();
                model.Tel = ds.Tables[0].Rows[0]["Tel"].ToString();
                model.Adress = ds.Tables[0].Rows[0]["Adress"].ToString();
                model.Zip = ds.Tables[0].Rows[0]["Zip"].ToString();
                    if (ds.Tables[0].Rows[0]["allowgroupbuydeposit"].ToString() != "")
                {
                    model.AllowGroupbuyDeposit = int.Parse(ds.Tables[0].Rows[0]["allowgroupbuydeposit"].ToString());
                }
                if (ds.Tables[0].Rows[0]["allowauctiondeposit"].ToString() != "")
                {
                    model.AllowAuctionDeposit = int.Parse(ds.Tables[0].Rows[0]["allowauctiondeposit"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        #endregion  成员方法
    }
}

